<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
     
      <el-form-item label="合作状态 " prop="cooperationStatus">
        <el-select v-model="queryParams.cooperationStatus" placeholder="请选择合作状态 " clearable>
          <el-option
            v-for="dict in merchant_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['manage:merchant:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['manage:merchant:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['manage:merchant:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="merchantList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="商家ID" align="center" prop="merchantId"  type="index" width="80"/>
      <el-table-column label="商家名称" align="center" prop="name" />
      <el-table-column label="合作状态 " align="center" prop="cooperationStatus">
        <template #default="scope">
          <dict-tag :options="merchant_status" :value="scope.row.cooperationStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="合作期限" align="center" prop="cooperationTerm" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.cooperationTerm, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['manage:merchant:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['manage:merchant:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改合作商家管理对话框 -->
<el-dialog :title="title" v-model="open" width="500px" append-to-body>
  <el-form ref="merchantRef" :model="form" :rules="rules" label-width="80px">
    <!-- 只读显示基本信息 -->
    <el-form-item label="商家名称">
      <span>{{ form.name }}</span>
    </el-form-item>
    <el-form-item label="手机号">
      <span>{{ form.phone }}</span>
    </el-form-item>
    <el-form-item label="地址">
      <span>{{ form.address }}</span>
    </el-form-item>
    <el-form-item label="所属区域">
      <span>{{ getRegionName(form.regionId) }}</span>
    </el-form-item>
    
    <!-- 可编辑字段 -->
    <el-form-item label="合作状态" prop="cooperationStatus">
      <el-radio-group v-model="form.cooperationStatus">
        <el-radio
          v-for="dict in merchant_status"
          :key="dict.value"
          :label="parseInt(dict.value)"
        >{{dict.label}}</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="合作期限" prop="cooperationTerm">
      <el-date-picker clearable
        v-model="form.cooperationTerm"
        type="date"
        value-format="YYYY-MM-DD"
        placeholder="请选择合作期限">
      </el-date-picker>
    </el-form-item>
  </el-form>
  <template #footer>
    <div class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </template>
</el-dialog>
  </div>
</template>

<script setup name="Merchant">
import { listMerchant, getMerchant, delMerchant, addMerchant, updateMerchant } from "@/api/manage/merchant"
import { listRegion } from "@/api/manage/region"
const { proxy } = getCurrentInstance()
const { merchant_status } = proxy.useDict('merchant_status')

const merchantList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")
const regionList = ref([])

// 获取区域列表
function getRegionList() {
  listRegion({}).then(response => {
    regionList.value = response.rows
  })
}
// 根据区域ID获取区域名称
function getRegionName(regionId) {
  const region = regionList.value.find(item => item.regionId === regionId)
  return region ? region.name : regionId
}
// 在组件初始化时获取区域数据
onMounted(() => {
  getRegionList()
  getList()
})
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    phone: null,
    cooperationStatus: null,
    regionId: null
  },
  rules: {
    name: [
      { required: true, message: "商家名称不能为空", trigger: "blur" }
    ],
    phone: [
      { required: true, message: "手机号不能为空", trigger: "blur" }
    ],
    address: [
      { required: true, message: "地址不能为空", trigger: "blur" }
    ],
    cooperationStatus: [
      { required: true, message: "合作状态 ", trigger: "change" }
    ],
    cooperationTerm: [
      { required: true, message: "合作期限不能为空", trigger: "blur" }
    ],
    regionId: [
      { required: true, message: "所属区域ID不能为空", trigger: "blur" }
    ]
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询合作商家管理列表 */
function getList() {
  loading.value = true
  listMerchant(queryParams.value).then(response => {
    merchantList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    merchantId: null,
    name: null,
    phone: null,
    address: null,
    cooperationStatus: null,
    cooperationTerm: null,
    createTime: null,
    updateTime: null,
    regionId: null
  }
  proxy.resetForm("merchantRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.merchantId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加合作商家管理"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _merchantId = row.merchantId || ids.value
  getMerchant(_merchantId).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改合作商家管理"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["merchantRef"].validate(valid => {
    if (valid) {
      if (form.value.merchantId != null) {
        updateMerchant(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addMerchant(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _merchantIds = row.merchantId || ids.value
  proxy.$modal.confirm('是否确认删除合作商家管理编号为"' + _merchantIds + '"的数据项？').then(function() {
    return delMerchant(_merchantIds)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('manage/merchant/export', {
    ...queryParams.value
  }, `merchant_${new Date().getTime()}.xlsx`)
}

getList()
</script>
